package com.lvan.service.common;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvan.bean.BaseException;
import com.lvan.bean.Goods;
import com.lvan.bean.GoodsLog;
import com.lvan.bean.Page;
import com.lvan.bean.User;
import com.lvan.constant.ValidateRecord;
import com.lvan.dao.mapper.GoodsLogMapper;
import com.lvan.dao.mapper.GoodsMapper;
import com.lvan.service.AbstractService;
import com.lvan.service.interfaze.common.IGoodsService;
import com.lvan.util.InputValidate;

@Service("goodsService")
public class GoodsService extends AbstractService implements IGoodsService {
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private GoodsLogMapper goodsLogMapper;
	private InputValidate<Goods> validator = new InputValidate<Goods>();
	@Override
	public void add(Goods o) throws BaseException {
		Set<ValidateRecord> s = validator.validate(o, true);
		if(!s.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			for (ValidateRecord constraintViolation : s) {
				sb.append(constraintViolation.getMessage() + "; ");
			}
			throw new BaseException("", REQUEST_PARAM_ERROR, sb.toString());
		}
		goodsMapper.add(o);
	}

	@Override
	public void delete(Serializable ids) throws BaseException {
		if(ids!=null)
			goodsMapper.delete(ids);
	}

	@Override
	public void update(Goods o,User u) throws BaseException {
		Set<ValidateRecord> s = validator.validate(o, false);
		if(!s.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			for (ValidateRecord constraintViolation : s) {
				sb.append(constraintViolation.getMessage() + "; ");
			}
			throw new BaseException("", REQUEST_PARAM_ERROR, sb.toString());
		}
		Goods old = this.getByID(o.getGoodsId());
		goodsMapper.update(o);
		if(o.getGoodsName()!=null&&!o.getGoodsName().equals(old.getGoodsName())) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品名称",old.getGoodsName(),o.getGoodsName(),new Date(),"更新--"+u.getUserName(),""));
		}
		if(o.getGoodsPrice()!=null&&o.getGoodsPrice()!=old.getGoodsPrice()) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品价格",old.getGoodsPrice().toString(),o.getGoodsPrice().toString(),new Date(),"更新--"+u.getUserName(),""));
		}
		if(o.getGoodsPrice2()!=null&&o.getGoodsPrice2()!=old.getGoodsPrice2()) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品价格2",old.getGoodsPrice2().toString(),o.getGoodsPrice2().toString(),new Date(),"更新--"+u.getUserName(),""));
		}
		if(o.getGoodsPrice1()!=null&&o.getGoodsPrice1()!=old.getGoodsPrice1()) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品价格1",old.getGoodsPrice1().toString(),o.getGoodsPrice1().toString(),new Date(),"更新--"+u.getUserName(),""));
		}
		if(o.getGoodsCurrentCost()!=null&&o.getGoodsCurrentCost()!=old.getGoodsCurrentCost()) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品成本",old.getGoodsCurrentCost().toString(),o.getGoodsCurrentCost().toString(),new Date(),"更新--"+u.getUserName(),""));
		}
		if(o.getBarCode()!=null&& !o.getBarCode().equals(old.getBarCode())) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品条码",old.getBarCode(),o.getBarCode(),new Date(),"更新--"+u.getUserName(),""));
		}
		if(o.getAssCode()!=null&&!o.getAssCode().equals(old.getAssCode())) {
			goodsLogMapper.add(new GoodsLog(o.getGoodsId(),"商品助记码",old.getAssCode(),o.getAssCode(),new Date(),"更新--"+u.getUserName(),""));
		}
	}

	@Override
	public Goods getByID(Serializable id) {
		return goodsMapper.getByID(id);
	}

	@Override
	public int count(Goods o) {
		return goodsMapper.count(o);
	}

	@Override
	public List<Goods> query(Goods o, Page page) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goods", o);
		if(page==null)
			page = new Page();
		params.put("page", page.getPageSql());
		params.put("orderBy", o.convertField2Column());
		page.setTotal(this.count(o));
		return goodsMapper.query(params);
	}

	@Override
	public void update(Goods t) throws BaseException {
		Set<ValidateRecord> s = validator.validate(t, false);
		if(!s.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			for (ValidateRecord constraintViolation : s) {
				sb.append(constraintViolation.getMessage() + "; ");
			}
			throw new BaseException("", REQUEST_PARAM_ERROR, sb.toString());
		}
		goodsMapper.update(t);
	}

}
